# -*- coding: utf-8 -*-
import scrapy
from urllib.parse import urljoin
import json
from first.items import TVItem


class TengxunSpider(scrapy.Spider):
    name = 'tengxun'
    start_urls = ['http://v.qq.com/x/list/tv']
    #  start_urls = ['http://v.qq.com/x/list/tv?&offset=4980']

    def parse(self, response):

        for tv in response.xpath(
                '/html/body/div[3]/div/div/div[1]/div[2]/div/ul/li'):
            item = TVItem()
            item['title'] = tv.xpath('div[1]/strong/a/text()').extract_first()
            item['iid'] = tv.xpath('div[1]/strong/a/@href').extract_first()
            content = dict()
            content['score'] = tv.xpath(
                'div[1]/div/em[1]/text()').extract_first(
                ) + tv.xpath('div[1]/div/em[2]/text()').extract_first()
            actors = []
            for tvp in tv.xpath('div[2]/a/text()'):
                actors.append(tvp.extract())
            content['actors'] = actors
            content['url'] = tv.xpath('div[1]/strong/a/@href').extract_first()
            #  item['content'] = json.dumps(content)
            item['content'] = content
            #  print(item['name'])
            #  print(item['score_l'])
            #  print(item['score_s'])
            #  print(item['actors'])
            yield item
        next_page = response.xpath(
            '/html/body/div[3]/div/div/div[2]/a[2]/@href').extract_first()
        if next_page != 'javascript:;':
            print(type(next_page))
            url = urljoin(response.url, next_page)
            yield scrapy.Request(url, callback=self.parse)
